package com.kunbo.cn.service.impl.system.user;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;
import com.bstek.uflo.process.assign.Entity;
import com.bstek.uflo.process.assign.PageQuery;
import com.kunbo.cn.dao.system.MenuDao;
import com.kunbo.cn.dao.system.rule.RoleRuleDao;
import com.kunbo.cn.dao.system.rule.RuleStructDao;
import com.kunbo.cn.dao.system.user.PositionDao;
import com.kunbo.cn.entity.system.Menu;
import com.kunbo.cn.entity.system.user.Position;
import com.kunbo.cn.service.system.user.PositionService;

@Service
public class PositionServiceImpl implements PositionService{

	@Autowired
	private PositionDao positionDao;
	
	@Autowired
	private MenuDao menuDao;
	
	@Autowired
	private RuleStructDao ruleStructDao;
	
	@Autowired
	private RoleRuleDao roleRuleDao; 
	
	private Logger logger = LoggerFactory.getLogger(PositionServiceImpl.class);
	
	@Override
	public List<Position> list() {
		// TODO Auto-generated method stub
		return positionDao.findAll();
	}

	@Override
	public Position save(Position position) {
		// TODO Auto-generated method stub
		
		logger.info("save");
		
		Position p = positionDao.save(position);
		
//		//存储activiti的group
//		String uuid = UUID.randomUUID().toString();
//		
//		positionDao.save(p);
//		
//		//向角色插入权限列表
//		List<RuleStruct>  list = ruleStructDao.findAll();
//		for(RuleStruct rs:list)
//		{
//			RoleRule r = new RoleRule();
//			r.setId(0);
//			r.setIs_use(false);
//			r.setPostation_id(p.getId());
//			r.setRule_struct_id(rs.getId());
//			roleRuleDao.save(r);
//		}
		return p;		
	}
	
	
	@Modifying
	@Transactional
	@Override
	public void delete(Position position) {
		// TODO Auto-generated method stub
		positionDao.delete(position);
		roleRuleDao.deleteBy_postation_id(position.getId());
	}

	@Override
	public Position findById(int id) {
		// TODO Auto-generated method stub
		return positionDao.findById(id);
	}

	@Override
	public Position update(Position position) {
		// TODO Auto-generated method stub		
		logger.debug("update");
		return positionDao.save(position);		
	}

	@Override
	public void page(PageQuery<Entity> pageQuery) {
		// TODO Auto-generated method stub
		
	}
    /**
     * 
     * <p>Title: getMenuByPositionId</p>
     * <p>Description:根据角色获取对应的菜单 </p>
     * @param positionId
     * @return
     * @see com.kunbo.cn.service.system.user.PositionService#getMenuByPositionId(java.lang.Integer)
     */
	@Override
	public List<Menu> getMenuByPositionId(Integer positionId) {
		List<Menu> list = positionDao.findById(positionId).getMenus();
		System.out.println(list.toString());
		//1.先获取一级菜单
		List<Menu> oneLevelMenu = new ArrayList<Menu>();
		for(Menu m:list) {
			if(m.getPid()==1) {
				oneLevelMenu.add(m);
			}
		}
		System.out.println(oneLevelMenu.toString());
		for(Menu menu : oneLevelMenu) {
			List<Menu> child = getChildMenu(menu.getId(), list);
			menu.setChildren(child);
			
		}
		return oneLevelMenu;
	}
	
	private List<Menu> getChildMenu(Integer id ,List<Menu> list) {
        //存放子菜单的集合
        List<Menu> listChild = new ArrayList<Menu>();
        for (Menu m : list) {
            if (m.getPid()==id) {
                listChild.add(m);
            }
        }
        if(listChild.size()>0) {//一级菜单存在子菜单
        	for (Menu mm : listChild) {
              mm.setChildren(getChildMenu(mm.getId(), list));
          }
        }else {
        	return null;
        }
        return listChild;
    }

	@Override
	public void authorityConfigure(String str) {
		Map map = (Map)JSON.parse(str);//将json串转换为map
		Integer id = (Integer) map.get("id");//获取角色id
		Position p = positionDao.findById(id);
		String authorityStr = (String) map.get("authorityStr");
		String[]  arr = authorityStr.split(",");
		List<Menu> list = new ArrayList<Menu>();
		for(int i=0;i<arr.length;i++) {
			list.add(menuDao.findOne(Integer.valueOf(arr[i])));
		}
		p.setMenus(list);
		p.setRemark(authorityStr);
		positionDao.save(p);
		
	}

}
